<template>
  <a-card class="br">
    <a-table
      class="antableClass"
      ref="table"
      size="middle"
      rowKey="key"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      @change="handleTableChange"
      :loading="loading"
      :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
    >
      <template slot="title">
        <div class="table-header">
          <span class="text">模型列表</span>
          <div>
            <a-button @click="resetConfig" class="clear_btn">再次配置</a-button>
            <a-button @click="exportResult" class="clear_btn">导出结果</a-button>
            <a-button icon="arrow-right" style="margin-left: 18px;" type="danger">转移点</a-button>
          </div>
        </div>
      </template>
    </a-table>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { postAction, getAction } from '@/api/manage'
export default {
  name: 'ResultPart',
  mixins: [JeecgListMixin],
  data() {
    return {
      url: {
        list: '/custom/queryCustomData',
        columnsTitle: '/custom/queryTitleConfig',
        export: '/custom/exportCustomData'
      },
      queryParam: {},
      dataSource: [],
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        }
      ],
      loading: false,
      disableMixinCreated: true
    }
  },
  mounted() {
    this.loadData()
    this.getColumns()
  },
  methods: {
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      // 加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams() // 查询条件
      this.loading = true
      // this.dataSource=[]
      postAction(this.url.list, params).then(res => {
        if (res.success) {
          // update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          this.dataSource = res.result.records || res.result
          if (res.result.total) {
            this.ipagination.total = res.result.total
          } else {
            this.ipagination.total = 0
          }
          // update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
        } else {
          this.$message.error(res.message)
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    getColumns() {
      this.columns = this.$options.data().columns
      getAction(this.url.columnsTitle)
        .then(res => {
          res.result.map(item => {
            this.columns.push({
              title: item.fieldCnName,
              dataIndex: item.fieldEnName,
              key: item.fieldEnName,
              align: 'center'
            })
          })
        })
        .catch(err => {})
    },
    resetConfig() {
      this.$emit('SetConfig')
    },
    exportResult() {
      if (this.dataSource.length != 0) {
        getAction(this.url.export, this.queryParam).then(res => {
          if (res.success) {
            this.handleDownloadFile(res.result)
          }
          if (res.code === 510) {
            this.$message.warning(res.message)
          }
        })
      } else {
        this.$message.warning('请先查询')
      }
    },
    handleDownloadFile(fileUrl) {
      this.$message.success('正在下载，请稍等！')
      let url = `${window._CONFIG['domianURL']}/sys/common/download/${fileUrl}`
      window.open(url, '_blank')
    }
  }
}
</script>

<style lang="less" scoped>
@import '~@assets/less/tableTheme.less';
.br {
  border-radius: 20px;
}
.table-header {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0px 20px;
  .text {
    font-size: 18px;
    color: #3d58a8;
  }
}
</style>
